/***
  cliente.java is part of Marioneta.

  Marioneta is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License.

  Marioneta is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
  
  @author martin
   Martin Edmundo Barriga Orozco, 2008
   martin.barriga@gulmore.org
  
  cliente: 
 */

package escritorioremoto;

import java.net.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
//import java.io.PrintStream; Chingadera de clase, no sirve
import java.io.OutputStream;

/**
 * @author martin
 * Martin Edmundo Barriga Orozco 2007
 * martin.barriga@gulmore.org
 * 
 */
public class cliente{
    String host;
    int port;
    boolean isRun=false;
    Socket skCliente;
    
    public cliente(String cte, int dport) {
         try{
            skCliente = new Socket( cte , dport );
            isRun = true;
         } 
         catch( Exception e ) {
            isRun = false;
            System.out.println("Error al crear el socket: " + e.getMessage() );
         }
    }
    
    public void send(String strSend) throws IOException {
        if( !strSend.equals("") ){
            OutputStream ps = skCliente.getOutputStream();
            DataOutputStream flujo= new DataOutputStream( ps );
            flujo.writeUTF( strSend );
        }
        else{
            OutputStream ps = skCliente.getOutputStream();
            DataOutputStream flujo= new DataOutputStream( ps );
            flujo.writeUTF( "algo paso" );
        }
    }
    
    public String recibe() throws IOException {
        String buffer="";
        DataInputStream datos = new DataInputStream( skCliente.getInputStream() );
        buffer = datos.readUTF();
        return buffer;
    }
    
    public void down() throws IOException {
        isRun = false;
        skCliente.close();
        
    }
    
    public boolean status(){
        return isRun;
    }
}